package org.jeecg.modules.basicWarehouse.service.impl;

import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.basicWarehouse.entity.ConnectionPosition;
import org.jeecg.modules.basicWarehouse.mapper.ConnectionPositionMapper;
import org.jeecg.modules.basicWarehouse.service.IConnectionPositionService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 接驳位
 * @Author: jeecg-boot
 * @Date:   2025-03-12
 * @Version: V1.0
 */
@Service
public class ConnectionPositionServiceImpl extends ServiceImpl<ConnectionPositionMapper, ConnectionPosition> implements IConnectionPositionService {
	
	@Autowired
	private ConnectionPositionMapper connectionPositionMapper;
	@Autowired
	private RedisUtil redisUtil;
	
	@Override
	public List<ConnectionPosition> selectByMainId(String mainId) {
		return connectionPositionMapper.selectByMainId(mainId);
	}

	@Override
	public List<Map<String, Object>> updateReadyStatus(List<Map<String, Object>> list) {
		for (Map<String, Object> map : list) {
			// 提前检查connectionPositions是否为空
			List<Map<String, Object>> connectionPositions = (List<Map<String, Object>>) map.get("data");
			if (connectionPositions == null || connectionPositions.isEmpty()) {
				continue;
			}

			for (Map<String, Object> connectionPosition : connectionPositions) {
				String cellCode = (String) connectionPosition.get("cell_code");
				String key = "cellCode:" + cellCode;

				// 使用局部变量保存从Redis获取的数据
				Object redisValue = redisUtil.hasKey(key) ? redisUtil.get(key) : 0;
				connectionPosition.put("ready", redisValue);
			}
		}
		return list;
	}

}
